package homework2;

import Class3.NoAddressException;
import chourse.Order;
import chourse.OrderDishes;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class OrderService11 {
    Order order = new Order();

    public void inputaddress() {

        InputStream in = System.in;
        InputStreamReader reader = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(reader);
        String str = null;
        System.out.println("本次下单的收货地址是：" + str);
        order.setAddress(str);
    }

    public Order createOrder(OrderDishes[] orderDishes) throws NoAddressException {
        if (order.getAddress() == null) {
            throw new NoAddressException();
        }
        Order order = new Order();
        double totalAmount = 0d;
        for (OrderDishes orderDishes1 : orderDishes) {
            double amount = orderDishes1.getDishes().getPrice() * orderDishes1.getCount();
            totalAmount = totalAmount + amount;
        }
        order.setTotalAmount(totalAmount);
        order.setDishes(orderDishes);
        return order;
    }

    public void confirmOrder(Order order) {
        System.out.println("---------");
        System.out.println("本次下单的商品信息是");
        System.out.println("-----");
        for (OrderDishes dish : order.getDishes()) {
            System.out.println("菜品名称：" + dish.getDishes().getName());
            System.out.println("菜品价格：" + dish.getDishes().getPrice());
            System.out.println("菜品数量：" + dish.getCount());
            System.out.println("-----");
        }
        System.out.println("本次需要支付的总金额是：" + order.getTotalAmount());
    }

    public void payOrder(Order order) throws NumberFormatException {
        boolean isTrue = false;
        while (!isTrue) {
            String str = null;
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
                str = bufferedReader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            double amount = Double.valueOf(str);
            try {
                amount = Integer.parseInt(str);
            } catch (Exception e) {
                e.printStackTrace();
                throw new NumberFormatException("数字格式异常");
            }
            if (amount == order.getTotalAmount()) {
                isTrue = true;
                System.out.println("金额正确，订单已完成");
            } else {
                System.out.println("亲，输错金额啦");

            }
        }
    }
}
